import toolApis from "@/tool/http/ToolApis.js"

export default {
	namespaced: true,
	state: {
		token: uni.getStorageSync("com-token"),
		userinfo:  uni.getStorageSync("com-userinfo")
	},
	mutations: {
		updateToken: (state, value) => {
			state.token = value;
			uni.setStorageSync("com-token", value);
		},
		updateUserInfo: (state, userinfo) => {
			state.userinfo = userinfo;
			uni.setStorageSync("com-userinfo", userinfo);
		}
	},
	actions: {
		async login(context, data) {
			let res = await toolApis.login(data);
			let userinfo = await toolApis.userinfo();
			context.commit("updateToken", res);
			context.commit("updateUserInfo", userinfo);
			return res;
		},
		logout(context) {
			context.commit("updateToken", undefined);
			context.commit("updateUserInfo", undefined);
		}
	}
}
